package cn.csc.module.product.config;

import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.util.List;

/**
 * rocketmq消息分区
 * @author liuchuang
 * @version 1.0
 * @date 2023/5/11 14:02
 * @modify
 */
@Component
public class OrderlyMessageQueueSelector implements MessageQueueSelector {

    @Override
    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
        BigInteger id = new BigInteger(((MessageHeaders) arg).get(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID).toString());
        //String tag = (String) ((MessageHeaders) arg).get(MessageConst.PROPERTY_TAGS);
        int index = id.remainder(BigInteger.valueOf(mqs.size())).intValue();

        return mqs.get(index);
    }

}
